package com.ezreal.ezchat.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.ezreal.ezchat.R;
import com.ezreal.ezchat.utils.permission.PermissionManager;
import com.suntek.commonlibrary.utils.TextUtils;
import com.suntek.commonlibrary.utils.ToastUtils;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class MsgPwdSettingDialog extends DialogFragment {

    private static final String OPEN_KEY = "open_key";
    public static final int MSG_PWD_OPEN = 0x01;
    public static final int MSG_PWD_CLOSE = 0x02;
    public static final int MSG_PWD_VERIFY = 0x03;
    public static final int MSG_PWD_RESET = 0x04;

    public static MsgPwdSettingDialog newInstance(@MsgPwdType int type) {
        Bundle bundle = new Bundle();
        bundle.putInt(OPEN_KEY, type);
        MsgPwdSettingDialog dialog = new MsgPwdSettingDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    private NoticeDialogListener dialogListener;
    @MsgPwdType
    private int type;

    private EditText editTextOne;
    private EditText editTextTwo;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            dialogListener = (NoticeDialogListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(
                    context.toString() + " must implement NoticeDialogListener");
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }
        type = bundle.getInt(OPEN_KEY);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(buildCustomView())
                .setTitle(getTitle())
                .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        switch (type) {
                            case MSG_PWD_OPEN:
                            case MSG_PWD_RESET:
                                openMsgPwd();
                                break;
                            case MSG_PWD_CLOSE:
                                closeMsgPwd();
                                break;
                            case MSG_PWD_VERIFY:
                                verifyMsgPwd();
                                break;
                            default:
                                break;
                        }
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialogListener.onDialogNegativeClick(MsgPwdSettingDialog.this);
                    }
                });
        return builder.create();
    }

    /**
     * 获取标题
     */
    private String getTitle() {
        int text;
        switch (type) {
            case MSG_PWD_OPEN:
                text = R.string.open_msg_pwd;
                break;
            case MSG_PWD_CLOSE:
                text = R.string.close_msg_pwd;
                break;
            case MSG_PWD_VERIFY:
                text = R.string.verify_msg_pwd;
                break;
            case MSG_PWD_RESET:
                text = R.string.reset_msg_pwd;
                break;
            default:
                return "";
        }
        return getResources().getString(text);
    }

    private View buildCustomView() {
        View view = getActivity().getLayoutInflater().inflate(R.layout.messgae_dialog, null);
        TextView textViewOne = view.findViewById(R.id.text_msg_pwd_one);
        TextView textViewTwo = view.findViewById(R.id.text_msg_pwd_two);
        editTextOne = view.findViewById(R.id.edit_msg_pwd_one);
        editTextTwo = view.findViewById(R.id.edit_msg_pwd_two);
        if (type == MSG_PWD_OPEN) {
            textViewOne.setText(R.string.open_msg_pwd_info_one);
            textViewTwo.setText(R.string.open_msg_pwd_info_two);
        } else if (type == MSG_PWD_CLOSE) {
            textViewTwo.setVisibility(View.GONE);
            editTextTwo.setVisibility(View.GONE);
            textViewOne.setText(R.string.close_msg_pwd_info);
        } else if (type == MSG_PWD_VERIFY) {
            textViewTwo.setVisibility(View.GONE);
            editTextTwo.setVisibility(View.GONE);
            textViewOne.setText(R.string.verify_msg_pwd_info);
        } else if (type == MSG_PWD_RESET) {
            textViewOne.setText(R.string.open_msg_pwd_info_one);
            textViewTwo.setText(R.string.open_msg_pwd_info_two);
        }
        return view;
    }

    /**
     * 验证消息密码
     */
    private void verifyMsgPwd() {
        String textOne = editTextOne.getText().toString().trim();
        if (PermissionManager.getInstance().checkPwdSuccess(textOne)) {
            dialogListener.onDialogPositiveClick(MsgPwdSettingDialog.this);
        } else {
            ToastUtils.showMessage(getActivity(), "密码输入错误");
            dialogListener.onDialogNegativeClick(MsgPwdSettingDialog.this);
        }
    }

    /**
     * 关闭消息密码
     */
    private void closeMsgPwd() {
        String textOne = editTextOne.getText().toString().trim();
        if (PermissionManager.getInstance().checkPwdSuccess(textOne)) {
            PermissionManager.getInstance().setMsgPwd(null);
        } else {
            ToastUtils.showMessage(getActivity(), "密码输入错误");
            dialogListener.onDialogNegativeClick(MsgPwdSettingDialog.this);
        }
    }

    /**
     * 开启消息密码
     */
    private void openMsgPwd() {
        String textOne = editTextOne.getText().toString().trim();
        String textTwo = editTextTwo.getText().toString().trim();
        if (TextUtils.equals(textOne, textTwo)) {
            PermissionManager.getInstance().setMsgPwd(textOne);
        } else {
            ToastUtils.showMessage(getActivity(), "密码输入不符合");
            dialogListener.onDialogNegativeClick(MsgPwdSettingDialog.this);
        }
    }

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({
            MsgPwdSettingDialog.MSG_PWD_OPEN, MsgPwdSettingDialog.MSG_PWD_CLOSE,
            MsgPwdSettingDialog.MSG_PWD_VERIFY, MsgPwdSettingDialog.MSG_PWD_RESET
    })
    public @interface MsgPwdType {

    }

    public interface NoticeDialogListener {
        void onDialogPositiveClick(DialogFragment dialog);

        void onDialogNegativeClick(DialogFragment dialog);
    }
}
